package com.xm.settlement.service.impl;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.plugins.Page;
import com.xm.settlement.commons.base.ApiSQLException;
import com.xm.settlement.commons.utils.PageInfo;
import com.xm.settlement.mapper.AppOrderManageMapper;
import com.xm.settlement.model.AppGoodsManage;
import com.xm.settlement.model.AppOrderManage;
import com.xm.settlement.model.AppShipManage;
import com.xm.settlement.model.form.AppOrderShipForm;
import com.xm.settlement.service.IAppGoodsManageService;
import com.xm.settlement.service.IAppOrderManageService;
import com.xm.settlement.service.IAppShipManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/3/2.
 */
@Service
public class AppOrderManageServiceImpl extends SuperServiceImpl<AppOrderManageMapper,AppOrderManage> implements IAppOrderManageService {

    @Autowired
    private AppOrderManageMapper appOrderManageMapper;
    @Autowired
    private IAppShipManageService appShipManageService;
    @Autowired
    private IAppGoodsManageService appGoodsManageService;


    @Override
    public void selectDataGrid(PageInfo pageInfo,Long Lid) {
        Page<AppOrderManage> page = new Page<AppOrderManage>(pageInfo.getNowpage(), pageInfo.getSize());
        List<AppOrderManage> list = appOrderManageMapper.selectAppOrderManageList(page,Lid);
        for (AppOrderManage appOrderManage:list ) {
            appOrderManage.setGoodsTprice(appOrderManage.getGoodsPrice()*appOrderManage.getGoodsNum());
        }
        pageInfo.setRows(list);
        pageInfo.setTotal(page.getTotal());
}

    @Override
    public void addOrder(AppOrderManage appOrderManage) {
        appOrderManage.setCreateId(appOrderManage.getUpdateId());
        appOrderManage.setCreateTime(new Date());
        appOrderManage.setOrderCode("O"+String.valueOf(System.currentTimeMillis()));
        AppShipManage appShipManage = new AppShipManage();
        appShipManage.setOrderCode(appOrderManage.getOrderCode());
        appShipManageService.insert(appShipManage);
        appOrderManageMapper.insert(appOrderManage);
    }

    /**
     *  发货管理
     *  1.减少库存
     *  3.修改状态
     * */
    @Override
    public void insertDeliverGoods(AppOrderShipForm form, Long userId) throws ApiSQLException{
        // order id 订单id
        AppOrderManage appOrderManage  =  appOrderManageMapper.selectById(form.getId());
        /////////////////////////////////////////////////////////////////////
       AppGoodsManage goods =  appGoodsManageService.selectById(appOrderManage.getGoodsId());
       if(goods.getAmount() < appOrderManage.getGoodsNum() ){
            throw new ApiSQLException( 8900,"发货数量小于库存数量，请及时补充");
       }
        goods.setAmount(goods.getAmount() - appOrderManage.getGoodsNum() );
        goods.setUpdateId(userId);
        goods.setUpdateTime(new Date());
        appGoodsManageService.updateById(goods);

        AppShipManage appShipManage = new AppShipManage();
        appShipManage.setCreateId(userId);
        appShipManage.setCreateTime(new Date());
        appShipManage.setExpressName(form.getExpressName());
        appShipManage.setExpressNum(form.getExpressNum());
        appShipManage.setOrderCode(appOrderManage.getOrderCode());
        appShipManage.setShipCode("S"+String.valueOf(System.currentTimeMillis()));
        appShipManage.setGoodsName(appOrderManage.getGoodsName());
        appShipManage.setGoodsTprice(appOrderManage.getGoodsTprice());
        appShipManageService.insert(appShipManage);

        appOrderManage.setOrderStatus(5);
        appOrderManage.setUpdateId(userId);
        appOrderManage.setUpdateTime(new Date());
        appOrderManage.setReceName(form.getReceName() );
        appOrderManage.setReceTel(form.getReceTel());
        appOrderManage.setReceAddress(form.getReceAddress());
        updateById(appOrderManage);

    }

    @Override
    public void editDeliverGoods(AppOrderShipForm form, Long userId) {
        AppOrderManage appOrderManage  =  appOrderManageMapper.selectById(form.getId());
        appOrderManage.setUpdateId(userId);
        appOrderManage.setUpdateTime(new Date());
        appOrderManage.setReceName(form.getReceName() );
        appOrderManage.setReceTel(form.getReceTel());
        appOrderManage.setReceAddress(form.getReceAddress());
        updateById(appOrderManage);
    }


}
